package cn.kevinking.auth.service.impl;

import cn.kevinking.auth.domain.LoginUserAdapter;
import cn.kevinking.auth.entity.BfzUser;
import cn.kevinking.auth.mapper.BfzUserMapper;
import cn.kevinking.auth.service.IBfzUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * user service impl
 *
 * @author kevinking
 * @since 13 Apr 2025
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BfzUserServiceImpl extends ServiceImpl<BfzUserMapper, BfzUser> implements IBfzUserService {

	private final BfzUserMapper userMapper;

	/**
	 * override spring security UserDetailsService interface to authenticate
	 *
	 * @param username username string
	 * @return UserDetails object
	 * @throws UsernameNotFoundException throw if username not found
	 */
	@Override
	public UserDetails loadUserByUsername(String username) {
		BfzUser bfzUser = userMapper.selectOne(this.lambdaQuery().eq(BfzUser::getUsername, username));
		if (bfzUser == null) {
			throw new UsernameNotFoundException(username);
		} else {
			return new LoginUserAdapter(bfzUser);
		}
	}
}
